<template>
  <div class="xtx-city" ref="target">
    <div class="select" :class="{ active }" @click="toggleDialog">
      <span v-if="!fullLocation" class="placeholder">请选择配送地址</span>
      <span v-else class="value">{{ fullLocation }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <!-- 展开选项部分 -->
    <div class="option" v-if="active">
      <div class="loading" v-if="logding"></div>
      <template v-else="!logding">
        <span class="ellipsis" v-for="i in currList" :key="i.code" @click="changeItem(i)">{{ i.name }}</span>
      </template>
    </div>
  </div>
</template>
<script lang="ts" >
import { ref, computed, reactive } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
export default {
  // 配送地址城市组件
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }
  },
  setup(props, content) {
    // 控制展开收起,默认收起
    const active = ref(false)
    // 存放城市的数据
    const cityData = ref([])
    // 是否加载数据
    // 获取数据渲染且加上加载中效果。
    const logding = ref(false)
    // 展开
    const openDialog = () => {
      active.value = true
      // 获取数据
      logding.value = true
      getCityData().then((data: any) => {
        cityData.value = data
        logding.value = false
      })
    }
    // // 收起
    const closeDialog = () => {
      active.value = false
    }
    // 切换展开收起
    const toggleDialog = () => {
      active.value ? closeDialog() : openDialog()
      // 清空选择结果
      for (const key in changeResult) {
        changeResult[key] = ''
      }
    }
    // 点击其他位置隐藏
    // 参数1:监听那个元素
    // 参数2:点击该元素外面触发的事件逻辑
    const target = ref(null)
    onClickOutside(target, () => {
      closeDialog()
    })
    // 存放用户选择的地址
    const changeResult = reactive({
      // 省
      provinceCode: '',
      provinceName: '',
      // 市
      cityCode: '',
      cityName: '',
      // 区
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })
    // 拿到当前地区的code
    const changeItem = (code) => {
      if (code.level === 0) {
        changeResult.provinceCode = code.code
        changeResult.provinceName = code.name
      }
      if (code.level === 1) {
        changeResult.cityCode = code.code
        changeResult.cityName = code.name
      }
      if (code.level === 2) {
        changeResult.countyCode = code.code
        changeResult.countyName = code.name
        changeResult.fullLocation = `${changeResult.provinceName} ${changeResult.cityName} ${changeResult.countyName}`
        // 关闭选择对话框
        closeDialog()
        content.emit('change', changeResult)
      }
    }
    // 通过计算属性去显示城市地区
    const currList = computed(() => {
      // 第一层默认清空下为省份和直辖市,行政区
      let currList = cityData.value
      // 拿到市
      if (changeResult.provinceCode) {
        currList = currList.find(p => p.code === changeResult.provinceCode).areaList
      }
      // 拿到区
      if (changeResult.cityCode) {
        currList = currList.find(p => p.code === changeResult.cityCode).areaList
      }
      return currList
    })
    return { toggleDialog, active, target, currList, logding, changeItem, changeResult }
  }
}
// 获取数据函数封装且支持缓存。
const getCityData = () => {
  // 获取城市数据
  // 1. 数据在哪里？https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json
  // 2. 何时获取？打开城市列表的时候，做个内存中缓存
  // 这个位置可能有异步操作，封装成promise
  const _window = window as any
  return new Promise((res, rej) => {
    //  在window下挂载一个变量来做缓存

    if (_window.cityData as Window) {
      // 有缓存时
      res(_window.cityData)
    } else {
      //  无缓存
      const url = 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
      axios.get(url).then((data: any) => {
        // 先保存一份到缓存中
        _window.cityData = data.data
        res(_window.cityData)
      })
    }
  })
  // 3. 怎么使用数据？定义计算属性，根据点击的省份城市展示
}

  // 加上一个vue - cli配置，处理图片为base64
</script>
<style lang="less" scoped>
@import '../../assets/styles/mixins.less';
@import '../../assets/styles/variables.less';

.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;

  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;

    &.active {
      background: #fff;
    }

    .placeholder {
      color: #999;
    }

    .value {
      color: #666;
      font-size: 12px;
    }

    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }

  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    >span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;

      &:hover {
        background: #f5f5f5;
      }
    }

    // 数据加载中的样式
    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;
    }
  }

}
</style>
